package com.detector.service.org;

import org.springframework.stereotype.Service;

import com.detector.common.DataBean;
import com.google.gson.GsonBuilder;


@Service
public class InterfaceJson{
	/**
	 * 接口返回CODE
	 */
	public static final String SUCCESS_CODE = "0";//成功
	public static final String FAIL_CODE ="1";//失败
	public static final String EXCEPTION_CODE = "-1";//代码异常
	
	class DataInfo{
		private String code;
		private String msg;
		private Object rs;
		
		public DataInfo() {
			super();
		}

		public DataInfo(String code, String msg) {
			super();
			this.code = code;
			this.msg = msg;
		}

		public DataInfo(String code) {
			super();
			this.code = code;
		}

		public DataInfo(String code, Object rs) {
			super();
			this.code = code;
			this.rs = rs;
		}

		public DataInfo(String code, String msg, Object rs) {
			super();
			this.code = code;
			this.msg = msg;
			this.rs = rs;
		}

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public String getMsg() {
			return msg;
		}

		public void setMsg(String msg) {
			this.msg = msg;
		}

		
		public Object getRs() {
			return rs;
		}

		public void setRs(Object rs) {
			this.rs = rs;
		}
	}


	public InterfaceJson() {
	}
	private GsonBuilder gb = new GsonBuilder();
	public String toJsonResult() {
		return gb.create().toJson(this);
	}

	public  String setSuccessResult(Object data) {
		return gb.create().toJson(new DataInfo(InterfaceJson.SUCCESS_CODE,new DataBean(data)));
	}
	public  String setSuccessResult(String msg ,Object data) {
		return gb.create().toJson(new DataInfo(InterfaceJson.SUCCESS_CODE, msg, new DataBean(data)));
	}
	
	public String setErrorResult(String msg, Object data) {
		return gb.create().toJson(new DataInfo(InterfaceJson.FAIL_CODE, msg, new DataBean(data)));
	}

	public String setErrorResult(String msg) {
		return gb.create().toJson(new DataInfo(InterfaceJson.FAIL_CODE, msg));
	}

	public  String setExceptionResult(String msg, Object data) {
		return gb.create().toJson(new DataInfo(InterfaceJson.EXCEPTION_CODE,msg,new DataBean(data)));
	}
	public  String setExceptionResult(String msg) {
		return gb.create().toJson(new DataInfo(InterfaceJson.EXCEPTION_CODE,msg));
	}
	public  String setSuccessResult() {
		return gb.create().toJson(new DataInfo(InterfaceJson.SUCCESS_CODE));
	}
}
